/*
* Copyright 2024 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/


/*********************
 *      INCLUDES
 *********************/
#include <stdio.h>
#include "lvgl.h"
#include "custom.h"
#include "lv_port.h"

/*********************
 *      DEFINES
 *********************/
/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**
 * Create a demo application
 */
static lv_timer_t* icon_flash_timer;

static void wifi_icon_flash_timer(lv_timer_t * t);

void custom_init(lv_ui *ui)
{
    icon_flash_timer = lv_timer_create(wifi_icon_flash_timer,500,ui->Home_img_wifi);
}

static void wifi_icon_flash_timer(lv_timer_t * t)
{
    static bool visual = false;
    lv_obj_t* wifi_icon = (lv_obj_t*)lv_timer_get_user_data(t);
    if(visual)
    {
        lv_obj_add_flag(wifi_icon,LV_OBJ_FLAG_HIDDEN);
        visual = false;
    }
    else
    {
        lv_obj_clear_flag(wifi_icon,LV_OBJ_FLAG_HIDDEN);
        visual = true;
    }
}

void wifi_display_style(lv_ui *ui,WIFI_STATE_STYLE e)
{
    lv_port_lock();
    switch(e)
    {
        case LV_WIFI_STYLE_CONNECTED:
            lv_timer_pause(icon_flash_timer);
            lv_obj_clear_flag(ui->Home_img_wifi,LV_OBJ_FLAG_HIDDEN);
            lv_label_set_text(ui->Home_label_net, "网络已连接");
            break;
        case LV_WIFI_STYLE_DISCONNECTED:
            lv_timer_pause(icon_flash_timer);
            lv_obj_add_flag(ui->Home_img_wifi,LV_OBJ_FLAG_HIDDEN);
            lv_label_set_text(ui->Home_label_net, "说“开始配网”进入配网模式");
            break;
        case LV_WIFI_STYLE_CONFIG:
            lv_timer_resume(icon_flash_timer);
            lv_label_set_text(ui->Home_label_net, "AP配网:ESP32-AP,192.168.5.1");
            break;
        default:break;
    }
    lv_port_unlock();
}

extern volatile int Home_clock_hour_value;
extern volatile int Home_clock_min_value;
extern volatile int Home_clock_sec_value;

void set_home_time(lv_ui *ui,int year,int month,int day,int hour,int min,int sec)
{
    lv_port_lock();
    Home_clock_hour_value = hour;
    Home_clock_min_value = min;
    Home_clock_sec_value = sec;
    char buf[16];
    lv_snprintf(buf,sizeof(buf),"%d/%02d/%02d", year, month,day);
    lv_label_set_text(ui->Home_datetext_date, buf);
    lv_port_unlock();
}

